const { defineConfig } = require('@vue/cli-service')
const path = require('path')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const CompressionPlugin = require('compression-webpack-plugin')


const isProduction = process.env.NODE_ENV === 'production';

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = defineConfig({
  transpileDependencies: true,
  publicPath: "./",
  productionSourceMap: false,
  devServer: {
    port: 9000,
  },
  chainWebpack: (config) => {
 
    config.plugins.delete('prefetch');
    config.plugins.delete('preload');

    config.resolve.alias
      .set('@', resolve('src'))
      .set('@less', resolve('src/assets/less/page'))
  },
  configureWebpack: (config) => {


    if (isProduction) {
      config.plugins.push(
        // 生产环境自动删除console
        new UglifyJsPlugin({
          uglifyOptions: {
            output: {
              comments: false
            },
            compress: {
              drop_console: true, 
              drop_debugger: false,
              pure_funcs: ['console.log']// 移除console
            },
            warnings: false,
          },
          sourceMap: false,
          parallel: true
        })
      );

      return {
        plugins: [
          new BundleAnalyzerPlugin(),
          new CompressionPlugin({
            test: /\.js$|\.html$|\.css/, //匹配文件名
            threshold: 102400, //对超过10k的数据进行压缩
            deleteOriginalAssets: false //是否删除原文件
          })
        ]
      };
    }
  }
})
